package com.base.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.base.comm.CommContent;
import com.base.po.voip.Agentinfoes;
import com.base.service.AgentinfoesService;
import com.base.service.LogsService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * 添加紧急控制信息服务端
 * 
 * @author f
 *
 */
@Controller
@RequestMapping("/admin")
public class add_Urgent {

	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private AgentinfoesService agentinfoesService; // 紧急信息

	// 添加紧急
	@RequestMapping(value = "/toAddUrgent", method = RequestMethod.POST)
	public @ResponseBody int toAddUrgent(HttpServletRequest request, Agentinfoes agentinfoes)
			throws IOException, ParseException {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		if (userid == null) return -1; // 未登录
		if (agentinfoes.getHits() == 1) { // 添加启动页 只能添加一张
			List<Agentinfoes> beanAds = agentinfoesService.selectByAll(new Agentinfoes(userid, true, 1)); // 获取启动页
			if (beanAds != null && beanAds.size() > 0) return -2; // 失败
		}
		agentinfoes.setId(Produce.getUUID()); // 设置参数 UUID
		agentinfoes.setAgentid(userid); // 代理商ID
		agentinfoes.setDatastatus(true); // 状态可用
		logsService.insert(CommContent.TYPE_CREATE, CommContent.LOGS_XIAOXI + "配置紧急广告信息",
				(String) request.getSession().getAttribute("userNick"), true, IP.getIpAddr(request));
		return agentinfoesService.insertSelective(agentinfoes); // 添加
	}
}
